﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WpfAGVSystem.Common
{
    /// <summary>
    /// 字符串类
    /// </summary>
    public class StringProcess
    {
        public static readonly int QuantityOfPreserveGroup = 3;
        public static readonly string SPLITERSTR_COMMA = ",";
        public static readonly string SPLITERSTR_SPACE = " ";
        public static readonly string UNKNOW = "?";
        public static readonly string TIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
        public static readonly string TIMEFORMAT_TIME = "HH:mm:ss";
        public static readonly string TIMEFORMAT_DB_DATE = "yyyy-MM-dd";
        public static readonly string TIMEFORMAT_FILENAME = "yyyy-MM-dd HH_mm_ss";
        public static readonly string TIMEFORMAT_DB_TIME = "HH:mm:ss";
        public static readonly string TIMEFORMAT_MS = "yyyy-MM-dd HH:mm:ss.fff";
        public static readonly string ROLL_MARK_EMPTY_BOX = "□";
        public static readonly string ROLL_MARK_NONE = "_";
        public static readonly string ROLL_MARK_FULL = "●";
        public static readonly string ROLL_MARK_EmptyAxis = "⊙";
        public static readonly string ROLL_MARK_Halflings = "◑";
        public static readonly string VACTRAY = "△";
        public static readonly string HALFTRAY = "";
        public static readonly string TEMPTRAY = "⊙";
        public static readonly string COLDTRAY = "▲";
        public static readonly string HOTTRAY = "■";
        public static readonly string CELLTRAY = "●";
        public static readonly string VACTRAY_DOWN = "▽";
        public static readonly string VBLF = "\r\n";
        public static readonly string LEFTALLOW = "←";
        public static readonly string RIGHTALLOW = "→";
        public static readonly string BACKWARDSTR = "";
        public static readonly string FOREWARDSTR = "";
        public static readonly string STOPSTR = "||";
        public static readonly string ROBOTMOVING_Y = "|";
        public static readonly string CHARGING = "☇";
        public static readonly string UPWARDSTR = "▲";
        public static readonly string DOWNWARDSTR = "▼";

        public static readonly string TaskLock = "TaskLock";
        public static readonly string GetTask = "GetTask";
        public static readonly string CreateTask = "CreateTask";


        public static readonly string DefaultUser = "Admin";
        public static readonly string DefaultPassword = "Admin@123";
        public static readonly char Delimiter = ',';
        public static readonly string TimeFormat = "yyyyMMddHHmmssfff";
        public static string NowTime
        {
            get
            {
                return DateTime.Now.ToString(TimeFormat);
            }
        }

        public static DateTime? RetNowTime(string time)
        {
            try
            {
                DateTime dt= DateTime.ParseExact(time, TimeFormat, System.Globalization.CultureInfo.InvariantCulture);
                return dt;
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string LoadMD5(string password)
        {
            MD5 md5 = MD5.Create();
            //需要将字符串转成字节数组
            byte[] buffer = Encoding.Default.GetBytes(password);
            //加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择
            byte[] md5buffer = md5.ComputeHash(buffer);
            string str = null;
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            foreach (byte b in md5buffer)
            {
                str += b.ToString("x2");
            }
            return str;
        }

        public static string[] Split(string str)
        {
            return string.IsNullOrEmpty(str)?new string[0]:str.Split(new char[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string Combination(IQueryable<int> number, char c)
        {
            string str = null;
            if (number == null) return null;
            foreach (var index in number)
            {
                str += $"{number}{c}";
            }
            return str;
        }
    }
}
